package com.demo.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.demo.util.BaiDuFaceUtil;
import com.demo.entity.FaceUser;
import com.demo.mapper.FaceUserMapper;
import com.demo.result.ApiResult;
import com.demo.service.FaceUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author yl
 * @since 2020-11-17
 */
@Service
public class FaceUserServiceImpl extends ServiceImpl<FaceUserMapper, FaceUser> implements FaceUserService {

    @Autowired
    BaiDuFaceUtil baiDuFaceUtil;
    @Autowired
    FaceUserMapper faceUserMapper;

    @Override
    public ApiResult add(String imagebase64, String name,String password) {
        String add = baiDuFaceUtil.add(imagebase64);
        JSONObject jsonObject = JSONObject.parseObject(add);
        String result = jsonObject.getString("result");
        if (result == null) {
            return ApiResult.fail("注册失败，请确定对准的是人脸");
        }
        JSONObject f = JSONObject.parseObject(result);
        System.out.println(f.toJSONString());
        FaceUser newFU = new FaceUser();
        newFU.setFaceId(f.getString("face_token"));
        newFU.setName(name);
        newFU.setCreateTime(new Date());
        newFU.setPassword(password);
        faceUserMapper.insert(newFU);
        System.out.println("<=========人脸注册成功========>" + result);
        return ApiResult.ok();
    }
}
